import Taro from '@tarojs/taro-rn';
import * as tslib_1 from "tslib";
import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, ScrollView, Image, Text } from "@tarojs/components-rn";
import { toJS } from 'mobx';
import { inject, observer } from "@tarojs/mobx-rn";
import { getRuntime, pageSize, subtime } from "../../utils/common";
import integralStyleSheet from "./integral_styles";

import bg_13 from "../../assets/img/bg_13.jpg";
var _styleSheet = integralStyleSheet;

function _getClassName() {
  var className = [];
  var args = arguments[0];
  var type = Object.prototype.toString.call(args).slice(8, -1).toLowerCase();

  if (type === 'string') {
    args = args.trim();
    args && className.push(args);
  } else if (type === 'array') {
    args.forEach(function (cls) {
      cls = _getClassName(cls).trim();
      cls && className.push(cls);
    });
  } else if (type === 'object') {
    for (var k in args) {
      k = k.trim();

      if (k && args.hasOwnProperty(k) && args[k]) {
        className.push(k);
      }
    }
  }

  return className.join(' ').trim();
}

function _getStyle(classNameExpression) {
  var className = _getClassName(classNameExpression);

  var classNameArr = className.split(/\s+/);
  var style = [];

  if (classNameArr.length === 1) {
    style.push(_styleSheet[classNameArr[0].trim()]);
  } else {
    classNameArr.forEach(function (cls) {
      style.push(_styleSheet[cls.trim()]);
    });
  }

  return style;
}

let Company = class Company extends Component {
  constructor(props) {
    super(props);
    this.state = {
      currentNavId: 1,
      items: [],
      totalCount: 0,
      pageIndex: 1,
      pageSize,
      totalIntegral: 0
    };
  }
  componentWillMount() {
    Taro.setNavigationBarTitle({ title: '我的积分' });
    let totalIntegral = this.$router.params.totalPoints || 0;
    this.setState({ totalIntegral });
    this.getUserPointRecords();
  }
  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }
  changeNavId(id) {
    this.setState({ currentNavId: parseInt(id), totalCount: 0, items: [] }, () => {
      this.getUserPointRecords();
    });
  }
  getUserPointRecords() {
    let { userStore } = this.props.stores;
    let { currentNavId, pageIndex, pageSize } = this.state;
    userStore.getUserPointRecords({
      userPointsType: currentNavId,
      pageIndex,
      pageSize,
      callback: res => {
        if (pageIndex === 1) {
          this.setState({ totalCount: toJS(res.totalCount), items: toJS(res.items) });
        } else {
          const { items } = this.state;
          items.push(...(res.items || []));
          this.setState({ totalCount: res.totalCount, items });
        }
      }
    });
  }
  onReachBottom() {
    let { totalCount, pageSize, pageIndex } = this.state;
    const { loading } = this.props.stores.homeStore;
    if (totalCount > pageIndex * pageSize && !loading) {
      this.setState({
        pageIndex: pageIndex + 1
      }, () => {
        this.getUserPointRecords();
      });
    }
  }
  render() {
    let { currentNavId, items, totalIntegral } = this.state;
    let userInfo = getRuntime().getUserInfo();
    return <View style={_styleSheet["my-my_integral"]}>
          <View>
             <View style={_styleSheet["header"]}>
                <Image src={bg_13} style={_styleSheet["img"]} />
                <View style={_styleSheet["desc"]}>当前可用积分</View>
                <View style={_styleSheet["total-integral"]}>{totalIntegral}</View>
             </View>

             <ScrollView scrollX={true} style={_styleSheet["scroll-view-nav"]}>
                <View onClick={this.changeNavId.bind(this, 1)} style={_getStyle(currentNavId === 1 ? 'scroll-item scroll-item-on' : 'scroll-item')}>获取<Text style={_styleSheet["line"]} /></View>
                <View onClick={this.changeNavId.bind(this, 2)} style={_getStyle(currentNavId === 2 ? 'scroll-item scroll-item-on' : 'scroll-item')}>使用<Text style={_styleSheet["line"]} /></View>
             </ScrollView>
            {items.length > 0 && items.map(item => {
          return <View style={_styleSheet["list-19-item"]}>
                    <View style={_styleSheet["l"]}>
                      <View style={_styleSheet["list-title"]}>{item.title}</View>
                      <View style={_styleSheet["list-info"]}>{subtime(item.creationTime)}</View>
                    </View>
                    <View style={[_styleSheet["r"], _styleSheet["apply-res"]]}>
                      {currentNavId === 1 && <Text style={[_styleSheet["money"], _styleSheet["color"]]}>+{item.points}</Text>}
                      {currentNavId === 2 && <Text style={[_styleSheet["money"], _styleSheet["color"]]}>-{item.points}</Text>}
                    </View>
                  </View>;
        })}
            {items.length <= 0 && <View style={_styleSheet["page-none-info"]}>
              {currentNavId === 1 && <View>暂时还没有获得积分哦~</View>}
              {currentNavId === 2 && <View>您还没有积分使用记录哦~</View>}
            </View>}
          </View>
      </View>;
  }
};
Company = tslib_1.__decorate([inject("stores"), observer], Company);
export default Company;